package org.zjvis.datascience.web.controller.knowledgeGraph;

import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.zjvis.datascience.common.vo.konwledgeGraph.constructionVO.ConceptRequestVO;
import org.zjvis.datascience.service.kg.KnowledgeGraphService;

import javax.validation.Valid;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

@RequestMapping("/knowledge/graph")
@RestController
@Validated
public class ConceptController {
    public static final Logger LOGGER = LoggerFactory.getLogger(KnowledgeGraphConstructionController.class);
    public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    public static final Function<Long, String> NOT_FOUND_BY_ID_MESSAGE
            = knowledgeGraphFileId -> String.format("knowledgeGraphFile(id=%s) not found", knowledgeGraphFileId);
    public static final Function<String, String> NOT_FOUND_BY_PUBLISH_NO
            = publishNo -> String.format("publishNo=%s not found", publishNo);
    @Value("${restful.knowledgeGraphConstruct.url:#{null}}")
    private String KG_SERVICE_API;
    private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient.Builder().build();
    @Autowired
    private KnowledgeGraphService knowledgeGraphService;


    @PostMapping("/concept/delete")
    public ResponseEntity<String> delete(@Valid @RequestBody ConceptRequestVO conceptRequestVO
    ) throws IOException {
        okhttp3.RequestBody formBody = okhttp3.RequestBody.create(MediaType.parse(org.springframework.http.MediaType.APPLICATION_JSON_VALUE), OBJECT_MAPPER.writeValueAsBytes(conceptRequestVO));
        Request request = new Request.Builder()
                .url(KG_SERVICE_API + "concept/delete")
                .post(formBody)
                .build();
        Call call = OK_HTTP_CLIENT.newCall(request);
        Response response = call.execute();
        return ResponseEntity.status(response.code()).body(response.body().string());
    }

    @PostMapping("/concept/save")
    public ResponseEntity<String> save(@Valid @RequestBody ConceptRequestVO conceptRequestVO
    ) throws IOException {
        okhttp3.RequestBody formBody = okhttp3.RequestBody.create(MediaType.parse(org.springframework.http.MediaType.APPLICATION_JSON_VALUE), OBJECT_MAPPER.writeValueAsBytes(conceptRequestVO));
        Request request = new Request.Builder()
                .url(KG_SERVICE_API + "concept/save")
                .post(formBody)
                .build();
        Call call = OK_HTTP_CLIENT.newCall(request);
        Response response = call.execute();
        return ResponseEntity.status(response.code()).body(response.body().string());
    }

    @GetMapping("/concept/id")
    public ResponseEntity<String> queryById(@Valid @RequestParam long knowledgeGraphId,
                                                    @Valid @RequestParam String id) {
        try {
            Map<String, String> params = new ConcurrentHashMap<>();
            params.put("knowledgeGraphId", String.valueOf(knowledgeGraphId));
            params.put("id", String.valueOf(id));
            Response response = knowledgeGraphService.queryKGByGet(params, "concept/id");
            return ResponseEntity.status(response.code()).body(response.body().string());
        } catch (IOException ioException) {
            throw new RuntimeException(ioException.getMessage());
        }
    }

    @GetMapping("/concept/name")
    public ResponseEntity<String> queryByNameFuzzy(  @Valid @RequestParam long knowledgeGraphId,
                                                      @Valid @RequestParam String name,
                                                      @Valid @RequestParam int pageNumber,
                                                      @Valid @RequestParam int pageSize) {
        try {
            Map<String, String> params = new ConcurrentHashMap<>();
            params.put("knowledgeGraphId", String.valueOf(knowledgeGraphId));
            params.put("name", String.valueOf(name));
            params.put("pageNumber", String.valueOf(pageNumber));
            params.put("pageSize", String.valueOf(pageSize));
            Response response = knowledgeGraphService.queryKGByGet(params, "concept/name");
            return ResponseEntity.status(response.code()).body(response.body().string());
        } catch (IOException ioException) {
            throw new RuntimeException(ioException.getMessage());
        }
    }

    @GetMapping("/concept/name/response/name")
    public ResponseEntity<String> queryByNameFuzzyRespondName(   @Valid @RequestParam long knowledgeGraphId,
                                                       @Valid @RequestParam String name,
                                                       @Valid @RequestParam int pageNumber,
                                                       @Valid @RequestParam int pageSize) {
        try {
            Map<String, String> params = new ConcurrentHashMap<>();
            params.put("knowledgeGraphId", String.valueOf(knowledgeGraphId));
            params.put("name", String.valueOf(name));
            params.put("pageNumber", String.valueOf(pageNumber));
            params.put("pageSize", String.valueOf(pageSize));
            Response response = knowledgeGraphService.queryKGByGet(params, "concept/name/response/name");
            return ResponseEntity.status(response.code()).body(response.body().string());
        } catch (IOException ioException) {
            throw new RuntimeException(ioException.getMessage());
        }
    }
    @GetMapping("/concept/all")
    public ResponseEntity<String> all(   @Valid @RequestParam long knowledgeGraphId,
                                                                 @Valid @RequestParam int pageNumber,
                                                                 @Valid @RequestParam int pageSize) {
        try {
            Map<String, String> params = new ConcurrentHashMap<>();
            params.put("knowledgeGraphId", String.valueOf(knowledgeGraphId));
            params.put("pageNumber", String.valueOf(pageNumber));
            params.put("pageSize", String.valueOf(pageSize));
            Response response = knowledgeGraphService.queryKGByGet(params, "concept/all");
            return ResponseEntity.status(response.code()).body(response.body().string());
        } catch (IOException ioException) {
            throw new RuntimeException(ioException.getMessage());
        }
    }

    @GetMapping("/concept/parents")
    public ResponseEntity<String> parents(       @Valid @RequestParam long knowledgeGraphId,
                                                                     @Valid @RequestParam String id) {
        try {
            Map<String, String> params = new ConcurrentHashMap<>();
            params.put("knowledgeGraphId", String.valueOf(knowledgeGraphId));
            params.put("id", String.valueOf(id));

            Response response = knowledgeGraphService.queryKGByGet(params, "concept/parents");
            return ResponseEntity.status(response.code()).body(response.body().string());
        } catch (IOException ioException) {
            throw new RuntimeException(ioException.getMessage());
        }
    }

    @GetMapping("/concept/parents/respond/name")
    public ResponseEntity<String> parentsRespondName(      @Valid @RequestParam long knowledgeGraphId,
                                                                    @Valid @RequestParam String id) {
        try {
            Map<String, String> params = new ConcurrentHashMap<>();
            params.put("knowledgeGraphId", String.valueOf(knowledgeGraphId));
            params.put("id", String.valueOf(id));
            Response response = knowledgeGraphService.queryKGByGet(params, "concept/parents/respond/name");
            return ResponseEntity.status(response.code()).body(response.body().string());
        } catch (IOException ioException) {
            throw new RuntimeException(ioException.getMessage());
        }
    }

    @GetMapping("/concept/children")
    public ResponseEntity<String> children(       @Valid @RequestParam long knowledgeGraphId,
                                                            @Valid @RequestParam String id) {
        try {
            Map<String, String> params = new ConcurrentHashMap<>();
            params.put("knowledgeGraphId", String.valueOf(knowledgeGraphId));
            params.put("id", String.valueOf(id));
            Response response = knowledgeGraphService.queryKGByGet(params, "concept/children");
            return ResponseEntity.status(response.code()).body(response.body().string());
        } catch (IOException ioException) {
            throw new RuntimeException(ioException.getMessage());
        }
    }

    @GetMapping("/concept/children/respond/name")
    public ResponseEntity<String> childrenRespondName(        @Valid @RequestParam long knowledgeGraphId,
                                                   @Valid @RequestParam String id) {
        try {
            Map<String, String> params = new ConcurrentHashMap<>();
            params.put("knowledgeGraphId", String.valueOf(knowledgeGraphId));
            params.put("id", String.valueOf(id));
            Response response = knowledgeGraphService.queryKGByGet(params, "concept/children/respond/name");
            return ResponseEntity.status(response.code()).body(response.body().string());
        } catch (IOException ioException) {
            throw new RuntimeException(ioException.getMessage());
        }
    }

}
